package com.itheima.d11_reflect_method;

import java.lang.reflect.Method;

/**
 * 目标：了解获取方法Method的方式
 */
public class Test {

    public static void main(String[] args) throws Exception {
        //1.获取Class对象
        Class<Student> cls = Student.class;

        //2.获取所有公共的成员方法
        Method[] fields = cls.getMethods(); //获取所有公共的方法
        for (Method field : fields) {
            System.out.println( field);
        }
        System.out.println("========");

        //3.获取所有声明定义的方法
        Method[] declaredMethods = cls.getDeclaredMethods(); //获取所有定义的方法
        for (Method declaredMethod : declaredMethods) {
            System.out.println( declaredMethod);
        }
        System.out.println("========");

        //4.获取指定的公共的构造方法
        Method eatMethod = cls.getMethod("eat");
        System.out.println( eatMethod );
        System.out.println("========");

        //5.获取指定的构造方法
        Method setAgeMethod = cls.getDeclaredMethod("setAge", int.class);
        System.out.println( setAgeMethod);

        //6.创建小猫对象：设置name=小花猫,price=500 ，调用eat方法
        Object student = cls.newInstance();

        Method setNameMethod = cls.getDeclaredMethod("setName", String.class);
        setNameMethod.setAccessible(true);
        setNameMethod.invoke(student, "张三");

        Method setPriceMethod = cls.getDeclaredMethod("setPrice", double.class);
        setPriceMethod.setAccessible(true);
        setPriceMethod.invoke(student, 3600);

        System.out.println( student );

        Method eatM = cls.getMethod("eat");
        eatM.invoke( student );
    }
}
